package com.rear.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

import com.rear.entity.MessageTypeEntity;
import com.rear.service.MessageTypeService;
import com.rear.service.SysUserService;
import com.rear.utils.PageUtils;
import com.rear.utils.R;


/**
 * 
 * 
 * @author October
 * @email chenzeming@9an-data.com
 * @date 2017-04-14 12:00:02
 */
@Controller
@RequestMapping("messagetype")
public class MessageTypeController {
	@Autowired
	private MessageTypeService messageTypeService;
	
	@Autowired
	private SysUserService sysUserService;
	
	
	@RequestMapping("/messagetype.html")
	public String list(){
		return "messagetype/messagetype.html";
	}
	
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	@RequiresPermissions("messagetype:list")
	public R list(Integer page, Integer limit,
			HttpServletRequest req,
			HttpServletResponse resp){
		Map<String, Object> map = new HashMap<>();
		map.put("offset", (page - 1) * limit);
		map.put("limit", limit);
		//渠道标识
		String username =req.getSession().getAttribute("username").toString();
		map.put("channelid", sysUserService.getappid(username));
		//查询列表数据
		List<MessageTypeEntity> messageTypeList = messageTypeService.queryList(map);
		int total = messageTypeService.queryTotal(map);
		
		PageUtils pageUtil = new PageUtils(messageTypeList, total, limit, page);
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@ResponseBody
	@RequestMapping("/info/{id}")
	@RequiresPermissions("messagetype:info")
	public R info(@PathVariable("id") Integer id){
		MessageTypeEntity messageType = messageTypeService.queryObject(id);
		
		return R.ok().put("messageType", messageType);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping("/save")
	@RequiresPermissions("messagetype:save")
	public R save(@RequestBody MessageTypeEntity messageType,
			HttpServletRequest req,
			HttpServletResponse resp){
		
		//渠道标识
		String username =req.getSession().getAttribute("username").toString();
		messageType.setChannelid(sysUserService.getappid(username));
		messageTypeService.save(messageType);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("messagetype:update")
	public R update(@RequestBody MessageTypeEntity messageType){
		messageTypeService.update(messageType);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("messagetype:delete")
	public R delete(@RequestBody Integer[] ids){
		messageTypeService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
